package threadDemo;

/**
 * @author Ajie
 * @date 1/7/25
 * @function
 */

import java.util.concurrent.atomic.AtomicInteger;

//无符号 AtomicByte（0 到 255）
public class AtomicUnsignedByte {

    public static void main(String[] args) {
        AtomicUnsignedByte atomicByte = new AtomicUnsignedByte(254);

        System.out.println("Initial value: " + atomicByte.get()); // 254

        int newValue1 = atomicByte.getAndIncrement();
        System.out.println("After increment: " + newValue1); // 255

        int newValue2 = atomicByte.getAndIncrement();
        System.out.println("After increment: " + newValue2); // 0（循环回最小值）

        int newValue3 = atomicByte.getAndIncrement();
        System.out.println("After increment: " + newValue3); // 1

        AtomicUnsignedByte atomicUnsignedByte = new AtomicUnsignedByte();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 256*4; i++) {
                    int andIncrement = atomicUnsignedByte.getAndIncrement();
                    try {
                        Thread.sleep(2);} catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println("AtomicUnsignedByte:run:1  "+(0xFF & andIncrement));
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 256*4; i++) {
                    int andIncrement = atomicUnsignedByte.getAndIncrement();
                    try {Thread.sleep(2);} catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println("AtomicUnsignedByte:run:2  "+(0xFF & andIncrement));
                }
            }
        }).start();
        //结果还是 255 ok
    }

    private final AtomicInteger value;

    // 构造函数，初始化为0
    public AtomicUnsignedByte() {
        this(0);
    }

    // 构造函数，初始化为指定值（0到255）
    public AtomicUnsignedByte(int initialValue) {
        if (initialValue < 0 || initialValue > 255) {
            throw new IllegalArgumentException("Value must be between 0 and 255");
        }
        this.value = new AtomicInteger(initialValue);
    }

    // 获取当前值（0到255）
    public int get() {
        return value.get();
    }

    // 设置新值（0到255）
    public void set(int newValue) {
        if (newValue < 0 || newValue > 255) {
            throw new IllegalArgumentException("Value must be between 0 and 255");
        }
        value.set(newValue);
    }

    // 原子自增，并在达到最大值后循环到0
    public byte getAndIncrement() {
        while (true) {
            int current = value.get();
            int next = (current == 255) ? 0 : current + 1;
            if (value.compareAndSet(current, next)) {
                return (byte) current;
            }
        }
    }

    @Override
    public String toString() {
        return Integer.toString(get());
    }
}